/*
Author : Nguyen Van Ty
Email  : ngvanty03@gmail.com
Date   : 3/4/2011 

*/

using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using BusinessObject;
using Utility;
using System.Xml;
namespace DataAccessLayer
{
    /// <summary>
    /// Define PhieuChuyenKhoDaoBase class. This class user for DataAccessLayer, contain 4 function Insert, Update, Search, Delete.
    /// If you want add new function, you must define in PhieuChuyenKhoDao class , not here.
    /// </summary>
    public class PhieuChuyenKhoDaoBase
    {
        private static ILog logger = null;
        #region Constructor
        /// <summary>
        /// Define Constructor.
        /// </summary>
        public PhieuChuyenKhoDaoBase() { }
        static PhieuChuyenKhoDaoBase()
        {
            //Init logger.
            logger = LoggingUtility.GetLogger(typeof(PhieuChuyenKhoDaoBase).FullName);
        }
        #endregion
        /// <summary>
        /// Read data from IDataReader, convert to PhieuChuyenKho Object.
        /// </summary>
        private static PhieuChuyenKho GetPhieuChuyenKhoByDataReader(IDataReader reader)
        {
            String methodName = "GetPhieuChuyenKhoByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                PhieuChuyenKho result = new PhieuChuyenKho();
                //read MaPhieuChuyenKho field data.

                result.MaPhieuChuyenKho = ManagerDao.Getint64DataFromDataReader(reader, "MaPhieuChuyenKho");

                //read ChungXe field data.

                result.ChungXe = ManagerDao.GetintDataFromDataReader(reader, "ChungXe");

                //read NguoiNhap field data.

                result.NguoiNhap = ManagerDao.GetstringDataFromDataReader(reader, "NguoiNhap");

                //read MaKhoChuyen field data.

                result.MaKhoChuyen = ManagerDao.GetintDataFromDataReader(reader, "MaKhoChuyen");

                //read MaKhoDen field data.

                result.MaKhoDen = ManagerDao.GetintDataFromDataReader(reader, "MaKhoDen");

                //read NgayNhap field data.

                result.NgayNhap = ManagerDao.GetdatetimeDataFromDataReader(reader, "NgayNhap");

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        /// <summary>
        /// Define function Insert for PhieuChuyenKho Object.
        /// </summary>
        /// <param name="obj">Object Insert.</param>
        /// <returns>
        ///    True: Successfull.
        ///    False: Process Error.
        ///</returns>
        public static bool Insert(PhieuChuyenKho obj)
        {
            bool result = false;
            String methodName = "InsertPhieuChuyenKho";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spInsertPhieuChuyenKho");

            //Add value "ChungXe"  paramater.
            ManagerDao.AddInParameter(cmd, "ChungXe", DbType.Int32,
                ConvertValueObject.CastObjValueToInsertDB(obj.ChungXe));
            //Add value "NguoiNhap"  paramater.
            ManagerDao.AddInParameter(cmd, "NguoiNhap", DbType.String,
                ConvertValueObject.CastObjValueToInsertDB(obj.NguoiNhap));
            //Add value "MaKhoChuyen"  paramater.
            ManagerDao.AddInParameter(cmd, "MaKhoChuyen", DbType.Int32,
                ConvertValueObject.CastObjValueToInsertDB(obj.MaKhoChuyen));
            //Add value "MaKhoDen"  paramater.
            ManagerDao.AddInParameter(cmd, "MaKhoDen", DbType.Int32,
                ConvertValueObject.CastObjValueToInsertDB(obj.MaKhoDen));
            //Add value "NgayNhap"  paramater.
            ManagerDao.AddInParameter(cmd, "NgayNhap", DbType.DateTime,
                ConvertValueObject.CastObjValueToInsertDB(obj.NgayNhap));
            //add paramater store return value.        
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                Int64 id = Int64.Parse(ManagerDao.GetParameterValue(cmd, "returnValue").ToString());

                if (id > 0)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }

        /// <summary>
        /// Define function Update for PhieuChuyenKho Object.
        /// </summary>
        /// <param name="obj">Object Update.</param>
        /// <returns>
        ///    True: Successfull.
        ///    False: Process Error.
        ///</returns>
        public static bool Update(PhieuChuyenKho obj)
        {
            bool result = false;
            String methodName = "UpdatePhieuChuyenKho";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spUpdatePhieuChuyenKho");


            //Add value "MaPhieuChuyenKho"  paramater.
            ManagerDao.AddInParameter(cmd, "MaPhieuChuyenKho", DbType.Int64, ConvertValueObject.CastObjValueToInsertDB(obj.MaPhieuChuyenKho));
            //Add value "ChungXe"  paramater.
            ManagerDao.AddInParameter(cmd, "ChungXe", DbType.Int32, ConvertValueObject.CastObjValueToInsertDB(obj.ChungXe));
            //Add value "NguoiNhap"  paramater.
            ManagerDao.AddInParameter(cmd, "NguoiNhap", DbType.String, ConvertValueObject.CastObjValueToInsertDB(obj.NguoiNhap));
            //Add value "MaKhoChuyen"  paramater.
            ManagerDao.AddInParameter(cmd, "MaKhoChuyen", DbType.Int32, ConvertValueObject.CastObjValueToInsertDB(obj.MaKhoChuyen));
            //Add value "MaKhoDen"  paramater.
            ManagerDao.AddInParameter(cmd, "MaKhoDen", DbType.Int32, ConvertValueObject.CastObjValueToInsertDB(obj.MaKhoDen));
            //Add value "NgayNhap"  paramater.
            ManagerDao.AddInParameter(cmd, "NgayNhap", DbType.DateTime, ConvertValueObject.CastObjValueToInsertDB(obj.NgayNhap));
            // add parameter store return value.
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                int rows = (int)ManagerDao.GetParameterValue(cmd, "returnValue");
                if (rows == 1)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }

            }
            catch (Exception e)
            {
                //write error Message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }
        /// <summary>
        /// Get Data PhieuChuyenKho object. 
        /// </summary>
        /// <param name="maPhieuChuyenKho" >This is primary key to get data.</param>
        /// <returns>PhieuChuyenKho object.</returns>

        public static PhieuChuyenKho Get(Int64 maPhieuChuyenKho)
        {
            String methodName = "GetPhieuChuyenKho";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                PhieuChuyenKho result = null;
                DbCommand dbCommnand = ManagerDao.GetStoredProcCommand("spGetPhieuChuyenKho");
                //Add value "MaPhieuChuyenKho"  paramater.
                ManagerDao.AddInParameter(dbCommnand, "MaPhieuChuyenKho", DbType.Int64, maPhieuChuyenKho);
                IDataReader reader = ManagerDao.ExecuteReader(dbCommnand);
                if (reader.Read())
                {
                    result = GetPhieuChuyenKhoByDataReader(reader);

                }
                reader.Close();
                return result;
            }
            catch (System.Exception e)
            {
                //write error message to log file.
                logger.Error("Get", e);
                return null;
            }
        }
        /// <summary>
        /// Get All PhieuChuyenKho object from table PhieuChuyenKho.
        /// </summary>
        /// <returns>List PhieuChuyenKho object.</returns>
        public static List<PhieuChuyenKhoResult> GetAll()
        {
            String methodName = "GetAll";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<PhieuChuyenKhoResult> listPhieuChuyenKho = new List<PhieuChuyenKhoResult>();
                DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spGetAllPhieuChuyenKho");

                using (IDataReader reader = ManagerDao.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        listPhieuChuyenKho.Add(GetPhieuChuyenKhoResultByDataReader(reader));
                    }
                    reader.Close();
                }

                return listPhieuChuyenKho;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }

        /// <summary>
        /// Search PhieuChuyenKho object in table PhieuChuyenKho with condition is t object.
        /// </summary>
        /// <param name="t">Condition for Search.</param>       
        /// <param name="totalRow">  total records in this table.</param>
        /// <returns>List PhieuChuyenKhoResult object.</returns>
        public static List<PhieuChuyenKhoResult> SearchPhieuChuyenKhoSomeCondition(PhieuChuyenKho t)
        {
            String methodName = "SearchPhieuChuyenKhoSomeCondition";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<PhieuChuyenKhoResult> listPhieuChuyenKho = new List<PhieuChuyenKhoResult>();
                DbCommand cmd = ManagerDao.GetStoredProcCommand("spSearchPhieuChuyenKhoByCondition");
                //Add value Condition  paramater.
                ManagerDao.AddInParameter(cmd, "Condition", DbType.String, ConvertValueObject.CastObjValueToInsertDB(t.Condition));

                //Add value PageIndex  paramater.
                ManagerDao.AddInParameter(cmd, "PageIndex", DbType.Int32, t.ObjectPaging.PageIndex);
                //Add value PageSize  paramater.
                ManagerDao.AddInParameter(cmd, "PageSize", DbType.Int32, t.ObjectPaging.PageSize);
                //Add value SortField  paramater.
                ManagerDao.AddInParameter(cmd, "SortField", DbType.String, t.ObjectSortOrder.SortField);
                //Add value SortExpression  paramater.
                ManagerDao.AddInParameter(cmd, "SortExpression", DbType.String, t.ObjectSortOrder.SortExpression);
                using (IDataReader reader = ManagerDao.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        listPhieuChuyenKho.Add(GetPhieuChuyenKhoResultByDataReader(reader));
                        if (t.ToltalRow <= 0)
                            t.ToltalRow = ManagerDao.Getint32DataFromDataReader(reader, "TotalRow");
                    }

                    reader.Close();
                }

                return listPhieuChuyenKho;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }
        /// <summary>
        /// Delete PhieuChuyenKho object. 
        /// </summary>
        /// <param name="MaPhieuChuyenKho" >This is primary key to delete.</param>
        /// <returns>True/False.</returns>

        public static bool Delete(Int64 MaPhieuChuyenKho)
        {
            String methodName = "DeletePhieuChuyenKho";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spDeletePhieuChuyenKho");
                //Add value "MaPhieuChuyenKho"  paramater.
                ManagerDao.AddInParameter(dbCommand, "MaPhieuChuyenKho", DbType.Int64, MaPhieuChuyenKho);
                //Add value returnValue  paramater.
                ManagerDao.AddParameter(dbCommand, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
                try
                {
                    ManagerDao.ExecuteNonQuery(dbCommand);
                    int parameterValue = (int)ManagerDao.GetParameterValue(dbCommand, "returnValue");
                    if (parameterValue == 1)
                    {
                        return true;
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                return false;
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                return false;
            }
        }
        /// <summary>
        /// Read data from IDataReader, convert to PhieuChuyenKhoResult Object.
        /// </summary>
        private static PhieuChuyenKhoResult GetPhieuChuyenKhoResultByDataReaderByLanguage(IDataReader reader, string LanguageId)
        {
            String methodName = "GetPhieuChuyenKhoResultByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                PhieuChuyenKhoResult result = new PhieuChuyenKhoResult();
                //read MaPhieuChuyenKho field data.
                result.MaPhieuChuyenKho = ManagerDao.Getint64DataFromDataReader(reader, "MaPhieuChuyenKho");

                //read ChungXe field data.
                result.ChungXe = ManagerDao.GetintDataFromDataReader(reader, "ChungXe");

                //read NguoiNhap field data.
                result.NguoiNhap = ManagerDao.GetstringDataFromDataReader(reader, "NguoiNhap");

                //read MaKhoChuyen field data.
                result.MaKhoChuyen = ManagerDao.GetintDataFromDataReader(reader, "MaKhoChuyen");

                //read MaKhoDen field data.
                result.MaKhoDen = ManagerDao.GetintDataFromDataReader(reader, "MaKhoDen");

                //read NgayNhap field data.
                result.NgayNhap = ManagerDao.GetdatetimeDataFromDataReader(reader, "NgayNhap");

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        /// <summary>
        /// Read data from IDataReader, convert to PhieuChuyenKhoResult Object.
        /// </summary>
        private static PhieuChuyenKhoResult GetPhieuChuyenKhoResultByDataReader(IDataReader reader)
        {
            String methodName = "GetPhieuChuyenKhoResultByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                PhieuChuyenKhoResult result = new PhieuChuyenKhoResult();
                //read MaPhieuChuyenKho field data.
                result.MaPhieuChuyenKho = ManagerDao.Getint64DataFromDataReader(reader, "MaPhieuChuyenKho");

                //read ChungXe field data.
                result.ChungXe = ManagerDao.GetintDataFromDataReader(reader, "ChungXe");
                result.TenChungXe = ManagerDao.GetstringDataFromDataReader(reader, "TenChungXe");

                //read NguoiNhap field data.
                result.NguoiNhap = ManagerDao.GetstringDataFromDataReader(reader, "NguoiNhap");

                //read MaKhoChuyen field data.
                result.MaKhoChuyen = ManagerDao.GetintDataFromDataReader(reader, "MaKhoChuyen");
                result.TenKhoChuyen = ManagerDao.GetstringDataFromDataReader(reader, "TenKhoChuyen");
                //read MaKhoDen field data.
                result.MaKhoDen = ManagerDao.GetintDataFromDataReader(reader, "MaKhoDen");
                result.TenKhoDen = ManagerDao.GetstringDataFromDataReader(reader, "TenKhoDen");

                //read NgayNhap field data.
                result.NgayNhap = ManagerDao.GetdatetimeDataFromDataReader(reader, "NgayNhap");

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }

    }
}